// import moment from "moment";
// import _ from "../../static/js/underscore-min.js";
import _ from "underscore";
import moment from "moment";

import Vue from 'vue';

import api from '../api';
import {
  mapState,
  mapActions
} from 'vuex'
// doTaskEdit,tagAdd,tagDel,tagSet,taskGet,taskAdd,taskSet,taskDel

export const mixinPc = {
  data() {
    return {
      addCardItemLoading: false,
      addCardItemTitle: '',
    }
  },
  methods: {
  	daysGet(sdata) {
				let _this = this;
				api.days(sdata).then(data => {
					if(data && data.errno == 0) {
						data = data.data;
						_this.$store.commit({
							type: "z39/daysDic",
							days: data,
							month: _this.showMonth
						});
					} else { //发生错误.
						_this.$message({
							message: data.errmsg,
							type: 'error'
						});
					}
				})
			},
    //拖拽添加
    dragAdd: function(event) {
    	
    	var _this=this;
      var tagKeyAdd = $(event.to).attr('tagKey');
      var tagVal = $(event.to).attr('tagVal');
      var id = $(event.clone).attr("taskId");
      var item = this.taskDic[id];
      this.doTagSet(item, tagKeyAdd, tagVal);
     
      // this.doTaskEdit(item, tagKeyAdd, tagVal); //tagKeyAdd == "due"
      if (this.$route.name == '项目任务' && this.pid) {
        var fromTagKey=$(event.from).attr('tagKey');
        var fromTagId = this.tagDic[id + '_'+fromTagKey] ? this.tagDic[id + '_'+fromTagKey].id : '';
        if(fromTagKey&&fromTagId){
        	this.tagDel({id:  fromTagId,key: fromTagKey}, true, item);
        }
        if (item.pid == this.pid) {
          return;
        }
        
        //this.doTaskEdit(item, 'pid',this.pid); //添加pid
        this.taskSet({ pid: this.pid, id: item.id }, item);
      }
      //拖动到今日任务，标签被选中，则添加标签
      if (this.$route.name == "今日任务" || this.$route.name == "单任务") {
      	 var fromTagKey=$(event.from).attr('tagKey');
     		if(fromTagKey=='none'){
     			
	        var tagArr = [];
	        $('#tagTmpList li.kanban-Item-container.active').each(function() {
	          tagArr.push({
	            taskid: id,
	            key: $(this).attr('tagKey'),
	            value: $(this).attr('value') || ''
	          })
	        })
	        
	         tagArr.forEach(function(obj) {
		          _this.doTagSet(item, obj.key, obj.value)
		       })
	          //单列表拖动要添加pid
	        if(this.$route.params.projectId){
	        		
		          _this.taskSet({ pid: _this.$route.params.projectId, id: item.id }, item);
	        }
       }
      }
      console.log(1)
      //四象限拖动
       if (this.$route.name == "今日任务" ){
       		var anotherKey1 = $(event.to).attr('anotherKey1');
      		var anotherValue1= $(event.to).attr('anotherValue1');
      	
      		var fromTagKey=$(event.from).attr('anotherKey1');
	        if(fromTagKey&&fromTagId){
	        	console.log(fromTagKey)
	        	this.tagDel({id:  fromTagId,key: fromTagKey}, true, item);
	        }
	        if(anotherKey1){
	        	this.doTagSet(item, anotherKey1, anotherValue1);
	        }
       }
    },
    //跳到单
    jumpToOneTask: function(params) {
      //            this.$emit('transformCircle','单任务')
      this.$router.push({
        name: '单任务',
        params: params
      })
    },
    addTaskWithDueKV: function(key, value, taskdata, isProject) {
	  if(isProject&& (this.pid=='' || this.pid== null)) return this.$message({message: '请选择项目新建任务',type: 'warning'});
      var tags = {};
      tags[key] = value;
      this.addTaskWithDue(tags, taskdata, isProject);
    },
    //    日月周年添加任务
    addTaskWithDue: function(tags, taskdata, isProject) {
      if (this.addCardItemLoading) {
        return
      };
      if (!this.addCardItemTitle) {
        this.$message({
          message: '请输入标题',
          type: 'warning'
        });
        return;
      }
      if (isProject && !taskdata.pid) {
        this.$message({
          message: '请选择项目',
          type: 'warning'
        });
        return;
      }
      this.addCardItemLoading = true;
      var params = {
        //uid: 1,
        title: this.addCardItemTitle
      }
      taskdata = taskdata || {};
      params = Object.assign(params, taskdata);
      this.taskAdd(params, tags).then(data => {
        this.addCardItemLoading = false;
        this.addCardItemTitle = ''
      }, data => {
        this.addCardItemLoading = false;
      });

    },
   showTmpDialog: function(tagItem) {
      if ((this.$route.name=='今日任务'||this.$route.name=='单任务')&&(tagItem.key == 'other' || tagItem.key == 'dev')) {
        this.$refs.tmpDialog.showDialog(tagItem);
      }else if(this.$route.name=='项目任务'){
      	if(!this.pid){
      		this.showNotice('请先选中项目或创建项目', 'warning')
      	}else{
      		this.$refs.tmpDialog.showDialog(tagItem,this.pid);
      	}
      	 
      }
    },
    //判断任务状态显示不同的颜色
    taskStatusClass: function(status) {
      switch (status) {
        case 'started':
          return 'doing';
        case 'toggle':
          return 'pause';
        case 'cancelled':
          return 'cancel';
        case 'done':
          return 'finish';
        default:
          return ''
          break;
      }
    },
    changeTaskStatusBox:function(item,status){
    	
    	item=this.taskDic[item.id];
    	var state='done';
    	if(status!=undefined){
    		if(!status){
    			state='due';
    		}
    	}else if(!item.finished){
    		state='due';
    	}
     	this.taskSet({ state:state, id: item.id }, item);
    }
  }

}
